import { ifUidInTeam } from './if_uid_in_team'
import { team } from '@src/db/src/models/battle/team'
import { user_attribute } from '@src/db/src/models/user/user_attribute'

/**
 * 加载玩家（或队伍）属性
 * @param uid 用户uid
 * @returns Promise<user_attribute[]> 队伍成员或单人属性数组
 */
export async function loadPlayerAttributes(uid: number): Promise<any[]> {
  // 判断是否在队伍中
  const inTeam = await ifUidInTeam(uid)
  if (inTeam) {
    // 查找所在队伍
    const teams = await team.findAllValues({})
    for (const t of teams) {
      if (Array.isArray(t.members) && t.members.includes(String(uid))) {
        // 提取队伍所有成员uid
        const uids = t.members.map((id: any) => Number(id))
        // 查询所有成员属性
        const attrs = await user_attribute.findAllValues({
          where: { uid: uids }
        })
        return attrs
      }
    }
    // 理论上不会走到这里
    return []
  } else {
    // 单人
    const attr = await user_attribute.findOneValue({ where: { uid } })
    return attr ? [attr] : []
  }
}
